package cn.rc.rcsystem.service.impl;

import cn.rc.rcsystem.domain.Shop;
import cn.rc.rcsystem.exception.GlobalException;
import cn.rc.rcsystem.exception.GlobalExceptionCodeEnum;
import cn.rc.rcsystem.feign.UserService;
import cn.rc.rcsystem.mapper.ShopMapper;
import cn.rc.rcsystem.result.JSONResult;
import cn.rc.rcsystem.service.IShopService;
import cn.rc.rcsystem.util.AssertUtil;
import cn.rc.rcsystem.util.BaiduAuditUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-09
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
    @Autowired
    private UserService userService;

    /**
     * 店铺注册
     * @param shop
     * @return
     */
    @Override
    public JSONResult register(Shop shop) {
        // 前端参数校验
        AssertUtil.isNotNull(shop,GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        // 百度ai校验
        AssertUtil.isTrue(BaiduAuditUtils.TextCensor(shop.getName()), GlobalExceptionCodeEnum.BAIDUAI_ERROR);
        AssertUtil.isTrue(BaiduAuditUtils.TextCensor(shop.getAddress()), GlobalExceptionCodeEnum.BAIDUAI_ERROR);
        // 判断用户是否是本平台用户
        JSONResult jsonResult = userService.getPhone(shop.getTel());
        AssertUtil.isTrue(jsonResult.isSuccess(),GlobalExceptionCodeEnum.USER_REGISTER_ERROR);
        AssertUtil.isNotNull(jsonResult.getData(), GlobalExceptionCodeEnum.USER_REGISTER_ERROR);
        // 判断店铺是否注册
        EntityWrapper<Shop> wrapper = new EntityWrapper<>();
        wrapper.eq("name",shop.getName());
        Shop shop1 = selectOne(wrapper);
        if(shop1!=null){
            if(shop1.getTel()==shop.getTel()){
                throw new GlobalException(GlobalExceptionCodeEnum.STORE_ALREADY_EXISTS_ERROR);
            }
        }
        insert(shop);
        return JSONResult.success();
    }
}
